package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.SysDictItemMapper;
import org.linlinjava.litemall.db.dao.SysDictMapper;
import org.linlinjava.litemall.db.dao.VendorEmployeeMapper;
import org.linlinjava.litemall.db.dao.VendorMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.domain.Vendor.Column;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysDictService {

    @Resource
    private SysDictMapper dictMapper;
    @Resource
    private SysDictItemMapper dictItemMapper;


    /**
     * 获取所有字典
     * @return
     * @param dict
     */
    public List<SysDict> listDict(SysDict dict) {
        SysDictExample example = new SysDictExample();
        if(null != dict.getDictCode()) {
            example.or().andDictCodeEqualTo(dict.getDictCode());
        }
        if (null != dict.getDictName()) {
            example.or().andDictNameEqualTo(dict.getDictName());
        }

        return dictMapper.selectByExample(example);
    }
    public SysDict getDictByName(String dictName) {
        SysDictExample example = new SysDictExample();
        example.or().andDictNameEqualTo(dictName);

        return dictMapper.selectOneByExample(example);
    }
    public SysDict getDictByCode(String dictCode) {
        SysDictExample example = new SysDictExample();
        example.or().andDictCodeEqualTo(dictCode);

        return dictMapper.selectOneByExample(example);
    }
    public SysDict getDictById(int dictId) {
        return dictMapper.selectByPrimaryKey(dictId);
    }

    public boolean createDict(SysDict dict) {
        return dictMapper.insert(dict) > 0;
    }

    public boolean updateDict(SysDict dict) {
        return dictMapper.updateByPrimaryKey(dict) > 0;
    }

    public boolean deleteDict(Integer dictId) {
        return dictMapper.deleteByPrimaryKey(dictId) > 0;
    }

    /**
     * 查询字典项
     * @param dictId
     * @return
     */
    public List<SysDictItem> listDictItems(int dictId) {
        SysDictItemExample example = new SysDictItemExample();
        example.or().andDictIdEqualTo(dictId);

        return dictItemMapper.selectByExample(example);
    }
    public SysDictItem getDictItemById(int dictItemId) {
        return dictItemMapper.selectByPrimaryKey(dictItemId);
    }

    public boolean createDictItem(SysDictItem item) {
        return dictItemMapper.insert(item) > 0;
    }

    public boolean updateDictItem(SysDictItem item) {
        return dictItemMapper.updateByPrimaryKey(item) > 0;
    }
    public boolean deleteDictItem(int dictItemId) {
        return dictItemMapper.deleteByPrimaryKey(dictItemId) > 0;
    }

    public List<SysDictItem> listItemsByDictName(String dictName) {
        SysDictItemExample example = new SysDictItemExample();
        example.or().andDictIdEqualTo(getDictByName(dictName).getId());
        return dictItemMapper.selectByExample(example);
    }

    public List<SysDictItem> listItemsByDictCode(String dictCode) {
        SysDictItemExample example = new SysDictItemExample();
        example.or().andDictIdEqualTo(getDictByCode(dictCode).getId());
        return dictItemMapper.selectByExample(example);
    }
}
